package com.weipu.common.facade.support.service.task;

import java.util.ArrayList;

import android.util.Log;

import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.model.CatelogModel;
import com.weipu.common.util.CacheUtil;
import com.weipu.common.util.GlobalConfig;
import com.weipu.common.util.Logger;

/**
*  获取指定目录下的一级子目录
* @param categoryId 目录ID
* @param isLoadFromCache 是否从缓存中加载
* @author Administrator
*/
public class GetChildCategoriesTask extends BaseServiceTask<ArrayList<CatelogModel>>
{
    /**
     * 
     */
    private static final long serialVersionUID = -14942711936973832L;
    
    private int categoryId;
    
    private boolean isLoadFromCache;
    
    public GetChildCategoriesTask(int categoryId, boolean isLoadFromCache)
    {
        this.categoryId = categoryId;
        this.isLoadFromCache = isLoadFromCache;
        
    }
    
    public boolean executeRequest()
    {
        return true;
    }
    
    public ArrayList<CatelogModel> execute()
    {
        ArrayList<CatelogModel> resultList = null;
        try
        {
            resultList = ProviderFactory.createProductProvider().GetChildCategories(categoryId, isLoadFromCache);
            
            //缓存数据状态: 当前目录下无子目录
            if (null == resultList || resultList.isEmpty())
            {
                CatelogModel model = new CatelogModel();
                model.setId(categoryId);
                model.setCacheUpdateTime(System.currentTimeMillis());
                GlobalConfig.getInstance().getCategoryMapping().put(String.valueOf(categoryId), model);//使用新列表数据存储，防止UI 对列表Clear、remove操作
            }
            
        }
        catch (Exception e)
        {
            Logger.w(TAG, "getProductProvider().GetChildCategories(int categoryId, boolean isLoadFromCache) fail.", e);
        }
        
        return resultList;
    }
    
    @Override
    protected void executeResponse(ArrayList<CatelogModel> resultObj)
    {
        //保存至本地
        cacheData(resultObj);
        
        //上报公共处理模块
        super.executeResponse(resultObj);
        
    };
    
    //保存本地数据
    private void cacheData(ArrayList<CatelogModel> result)
    {
        if (null != result && !result.isEmpty())
        {
            //刷新内存缓存(如果开启的话)
            if (CacheUtil.getInstance().isEnableCategoryMemCache())
            {
                GlobalConfig.getInstance()
                    .getSubCategoryList()
                    .put(String.valueOf(categoryId), new ArrayList<CatelogModel>(result));
            }
            
            //刷新本地数据库缓存
            if (CacheUtil.getInstance().isEnableLocaleDBCache())
            {
                //清理缓存数据
                ProviderFactory.createCategoryServiceDBProvider()
                    .deleteCategoryListByParentID(String.valueOf(categoryId));
                
                for (int i = 0; i < result.size(); i++)
                {
                    ProviderFactory.createCategoryServiceDBProvider().saveCategoryInfo(result.get(i));
                }
            }
        }
    }
    
    @Override
    public boolean equals(Object o)
    {
        if (!(o instanceof GetChildCategoriesTask))
        {
            return false;
        }
        
        //        this.categoryId = categoryId;
        //        this.isLoadFromCache = isLoadFromCache;
        return this.toString().equals(o.toString());
    }
    
    //    equals 返回 true的链各个对象则hashcode必须相等
    @Override
    public int hashCode()
    {
        return toString().hashCode();
    }
    
    @Override
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("categoryId: ").append(categoryId).append(',');
        sb.append("isLoadFromCache: ").append(isLoadFromCache);
        return sb.toString();
    }
}
